$(function(){

		//jquery tabs
		$( "#tabs" ).tabs();
		//$( "#tabs" ).tabs({"active":$number});
		
		
		
		//initiate dataTables plugin
		 myTable = 
		$('#dynamic-table').DataTable( {
			
			"order": [[ 1, "desc" ]],  //默认字段排序
			
			//设置禁止字段排序			
			'columnDefs':[
				  {"targets": [ 0,0],"orderable": false },
				  {"targets": [ 0,2],"orderable": false },
				  {"targets": [ 0,5],"orderable": false },
				  {"targets": [ 0,6],"orderable": false },
			      {"targets": [ 0,7],"orderable": false },
			      {"targets": [ 0,8],"orderable": false },
			      {"targets": [ 0,9],"orderable": false },
				
			 ],
			
			"processing": true,		  //开启本地分页
			"serverSide": true,       //开启服务器模式
			"ajax": {
				"url": "index.php?r=deliveryinfo/datasource",   //ajax异步提取数据
				"type": "post",
				"data":function ( d ) {
				      return $.extend( {}, d, {
				        "state": $('#state').val(),
				      } );
				    }
			},
			
			//设置显示的列字段
			columns: [
				{ "data": "check" },
				{ "data": "ID" },
				{ "data": "DeliveryName" },					
				{ "data": "DeliveryID" },				
				{ "data": "LinkName" },
				{ "data": "Address" },
				{ "data": "LinkTel" },
				{ "data": "QQ" },
				{ "data": "Email" },
				{ "data": "options" },
						
			],
			
			//语言汉化
			language: {
				'url':'aceAsset/i18n/zh_cn.json',
			},
						
	    } );
	
		
		
		$.fn.dataTable.Buttons.swfPath = "aceAsset/js/dataTables/extensions/buttons/swf/flashExport.swf"; //in Ace demo aceAsset will be replaced by correct assets path
		$.fn.dataTable.Buttons.defaults.dom.container.className = 'dt-buttons btn-overlap btn-group btn-overlap';
		
		new $.fn.dataTable.Buttons( myTable, {
			buttons: [
			  {
				"extend": "colvis",
				"text": "<i class='fa fa-search bigger-110 blue'></i> <span class='hidden'>Show/hide columns</span>",
				"className": "btn btn-white btn-primary btn-bold",
				columns: ':not(:first):not(:last)'
			  },
			  {
				"extend": "copy",
				"text": "<i class='fa fa-copy bigger-110 pink'></i> <span class='hidden'>Copy to clipboard</span>",
				"className": "btn btn-white btn-primary btn-bold"
			  },
			  {
				"extend": "csv",
				"text": "<i class='fa fa-database bigger-110 orange'></i> <span class='hidden'>Export to CSV</span>",
				"className": "btn btn-white btn-primary btn-bold"
			  },
			  {
				"extend": "excel",
				"text": "<i class='fa fa-file-excel-o bigger-110 green'></i> <span class='hidden'>Export to Excel</span>",
				"className": "btn btn-white btn-primary btn-bold"
			  },
			  {
				"extend": "pdf",
				"text": "<i class='fa fa-file-pdf-o bigger-110 red'></i> <span class='hidden'>Export to PDF</span>",
				"className": "btn btn-white btn-primary btn-bold"
			  },
			  {
				"extend": "print",
				"text": "<i class='fa fa-print bigger-110 grey'></i> <span class='hidden'>Print</span>",
				"className": "btn btn-white btn-primary btn-bold",
				autoPrint: false,
				message: 'This print was produced using the Print button for DataTables'
			  }		  
			]
		} );
		myTable.buttons().container().appendTo( $('.tableTools-container') );
		
		//style the message box
		var defaultCopyAction = myTable.button(1).action();
		myTable.button(1).action(function (e, dt, button, config) {
			defaultCopyAction(e, dt, button, config);
			$('.dt-button-info').addClass('gritter-item-wrapper gritter-info gritter-center white');
		});
		
		
		var defaultColvisAction = myTable.button(0).action();
		myTable.button(0).action(function (e, dt, button, config) {
			
			defaultColvisAction(e, dt, button, config);
			
			
			if($('.dt-button-collection > .dropdown-menu').length == 0) {
				$('.dt-button-collection')
				.wrapInner('<ul class="dropdown-menu dropdown-light dropdown-caret dropdown-caret" />')
				.find('a').attr('href', '#').wrap("<li />")
			}
			$('.dt-button-collection').appendTo('.tableTools-container .dt-buttons')
		});
	
		////
	
		setTimeout(function() {
			$($('.tableTools-container')).find('a.dt-button').each(function() {
				var div = $(this).find(' > div').first();
				if(div.length == 1) div.tooltip({container: 'body', title: div.parent().text()});
				else $(this).tooltip({container: 'body', title: $(this).text()});
			});
		}, 500);
		
		
		
		myTable.on( 'select', function ( e, dt, type, index ) {
			if ( type === 'row' ) {
				$( myTable.row( index ).node() ).find('input:checkbox').prop('checked', true);
			}
		} );
		myTable.on( 'deselect', function ( e, dt, type, index ) {
			if ( type === 'row' ) {
				$( myTable.row( index ).node() ).find('input:checkbox').prop('checked', false);
			}
		} );
	
	
		/////////////////////////////////
		//table checkboxes
		$('th input[type=checkbox], td input[type=checkbox]').prop('checked', false);
		
		//select/deselect all rows according to table header checkbox
		$('#dynamic-table > thead > tr > th input[type=checkbox], #dynamic-table_wrapper input[type=checkbox]').eq(0).on('click', function(){
			var th_checked = this.checked;//checkbox inside "TH" table header
			
			$('#dynamic-table').find('tbody > tr').each(function(){
				var row = this;
				if(th_checked) myTable.row(row).select();
				else  myTable.row(row).deselect();
			});
		});
		
		//select/deselect a row when the checkbox is checked/unchecked
		$('#dynamic-table').on('click', 'td input[type=checkbox]' , function(){
			var row = $(this).closest('tr').get(0);
			if(!this.checked) myTable.row(row).deselect();
			else myTable.row(row).select();
		});
										
})
	

	/**
	 *自定义JS
	 **/
	
	//override dialog's title function to allow for HTML titles
	$.widget("ui.dialog", $.extend({}, $.ui.dialog.prototype, {
		_title: function(title) {
			var $title = this.options.title || '&nbsp;'
			if( ("title_html" in this.options) && this.options.title_html == true )
				title.html($title);
			else title.text($title);
		}
	}));
	
	
	/*
	 * 修改物流商信息
	 */
	function alter(id){
		
		//ajax异步请求修改视图界面																								
		$.ajax({
			type:"post",
			url:"index.php?r=deliveryinfo/alter",
			data:{
				action:"view",   //表明是请求查看修改的界面
				alterid:id,		//把表单ID传给alter修改视图	
			},
			
			success:function(msg){	
				$("#dialog-alter").remove();  //把dialog的内容移除清空
				$("#alter").html(msg); 		  //显示出返回的数据
								
				//dialog处理
				var dialog = $( "#dialog-alter" ).removeClass('hide').dialog({
					modal: true,
					title: '<div class="widget-header widget-header-small" ><h4 class="smaller"><i class="ace-icon fa fa-check"></i> 修改订单</h4></div>',
					width:600,		
					title_html: true,
					buttons: [ 						
							    {
									text: "确认",
									"class" : "btn btn-primary btn-minier",
									 click: function() {	
										 $("#alterd").submit();		  //提交表单数据								 																		
									} 
							    },
							    {
									text: "取消",
									"class" : "btn btn-minier",
									click: function() {										
										$( this ).dialog( "close" );  //关闭dialog
									} 
								}
							]
					});	
				
				//获取生成助记码的值	 
				$('#deliveryname').keyup(function(){					
					$.ajax({
						type:"post",
						url:"index.php?r=deliveryinfo/alter",			
						data:{
							action:"zhuji",
							value:$(this).val(),
						},
						success:function(msg){								
								$('#deliveryab').empty().val(msg);						
						}	
					});				
				});				
			}										
		});					
	}
	
	
	/*
	 * 单条删除物流商信息
	 */	
	function delet(id){
		bootbox.setLocale("zh_CN");
		bootbox.confirm("是否确定删除?", function(result) {
			if(result) {
				$.ajax({
					type:"post",
					url:"index.php?r=deliveryinfo/delet",
					data:{
						action:"delet",
						deliveryid:id,
					},
					dataType:'json',
					success:function(msg){
						if(msg == "删除失败")
							alert(msg);
						else
							myTable.draw( false );
					}					
				})
			}
		});		
	}
	
	
	/*
	 * 物流商多条删除
	 */	
	function deletAll(){
		
		bootbox.setLocale("zh_CN");
		bootbox.confirm("是否确定全部删除?", function(result) {
			if(result) {
				$("#deletall").submit();				
			}
		});
		
	}
	
	
	//获取生成助记码的值	 
	$('#deliveryinfoform-deliveryname').keyup(function(){					
		$.ajax({
			type:"post",
			url:"index.php?r=deliveryinfo/index",			
			data:{
				action:"zhuji",
				value:$(this).val(),
			},
			success:function(msg){								
					$('#deliveryinfoform-deliveryab').empty().val(msg);						
			}	
		});				
	});	
	
